<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="close">
    <!-- 表单 -->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="showRow = true"
        />
        <el-row v-show="showRow" class="tree-row">
          <i class="el-icon-error" @click="showRow = false"></i>
          <el-tree
            ref="tree"
            :data="deptList"
            :props="{ label: 'name' }"
            @node-click="handleNodeClick"
          ></el-tree>
        </el-row>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="$emit('update:showDialog', false)"> 取消 </el-button>
      <el-button type="primary" @click="addEmployee">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeDate, parseTime } from '@/utils/index.js'
import { addEmployee } from '@/api/employee.js'
import EnumerationEmployee from '@/api/constant/employees.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 展示树形部门
      showRow: false,
      // 部门树形列表
      deptList: [],
      // 聘用形式绑定
      hireType: EnumerationEmployee.hireType,
      // 表单双向绑定值
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      // 效验规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getDepartments()
  },
  methods: {
    // 点击了树形节点
    handleNodeClick (curr) {
      // 形参:当前点击节点部门的信息
      console.log(curr)
      // 关闭树形
      this.showRow = false
      // 双向绑定给input表单
      this.formData.departmentName = curr.name
    },
    // 获取组织架构方法
    async getDepartments () {
      const { depts } = await getDepartments()
      // this.deptList = depts
      // 把组织架构信息转换为树形组件信息
      this.deptList = tranListToTreeDate(depts)
      // console.table(tranListToTreeDate(depts))
    },
    // 弹窗关闭事件
    close () {
      //  子传父  用sync语法糖
      this.$emit('update:showDialog', false)
      // 清除效验规则和 有效验规则的表单内容
      this.$refs.addForm.resetFields()
    },

    /**
     * 新增员工
     * 1.先表单整体效验
     * 2.效验成功调用新增接口同步数据
     * 3.关闭弹窗===>清空效验规则和表单
     */
    async addEmployee () {
      try {
        await this.$refs.addForm.validate()
        // 在调新增员工接口时先把时间格式化
        this.formData.timeOfEntry = parseTime(this.formData.timeOfEntry, '{y}-{m}-{d}')
        this.formData.correctionTime = parseTime(this.formData.correctionTime, '{y}-{m}-{d}')
        await addEmployee(this.formData)
        // 添加成功 重刷列表===>调用父组件方法
        // console.log('获取到的父组件实例', this.$parent)
        this.$parent.getEmployeeList()
        // 关闭dialog ===> 清空效验规则
        this.$parent.showDialog = false
      } catch (error) {
        console.dir(error)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.tree-row {
  position: relative;
  width: 300px;
  border: 1px dotted;
  .el-icon-error {
    position: absolute;
    right: 5px;
    top: 5px;
    width: 10px;
    z-index: 5;
    cursor: pointer;
  }
}
</style>
